<?php

namespace app\admin\model\business;

use app\admin\model\basic\Assets;
use app\admin\model\basic\Goods;
use app\admin\model\report\Finance;
use think\Model;
use traits\model\SoftDelete;

class StashDetail extends Model
{
    use SoftDelete;


    // 表名
    protected $name = 'stash_detail';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    //状态:100=销售单,200=销售退货单,300=零售单,400=进货单,500=进货退货单
    public const STATUS_SH = 100; //销售单
    public const STATUS_SHTH = 200; //销售退货单
    public const STATUS_LS = 300; //零售单
    public const STATUS_JH = 400; //进货单
    public const STATUS_JHTH = 500; //进货退货单


    public const STATUS_ARR = [
        self::STATUS_SH => '销售单',
        self::STATUS_SHTH => '销售退货单',
        self::STATUS_LS => '零售单',
        self::STATUS_JH => '进货单',
        self::STATUS_JHTH => '进货退货单',
    ];

    /**
     * Notes: 进货单
     * User: AerYYY
     * Date: 2/5/24 11:04 AM
     * @param $params
     * @return bool
     */
    public function createDetails($params): bool
    {
        $goods_id = $params['goods_id'];
        $goods_qty = $params['goods_qty'];
        $goods_price = $params['goods_price'];
        $remarks = $params['goods_remarks'];

        $price = 0;
        foreach ($goods_id as $key => $item) {
            if ($item && !empty($goods_qty[$key]) && $goods_qty[$key] > 0) {
                $price += bcmul($goods_qty[$key], $goods_price[$key], 2);

                $goodsModel = new Goods();
                $goods = $goodsModel->where('id', $item)->find();
                if (empty($goods)) {
                    $this->error = '错误的商品数据';
                    return false;
                }
                $goods->qty = $goods->qty + $goods_qty[$key];
                $goods->save();

                $stashGoodsDetails = new StashGoodsDetail();

                $stash_surplus_qty = 0;
                $stash_last = $stashGoodsDetails->where(['goods_id' => $item, 'stash_id' => $params['stash_id']])->order('createtime desc')
                    ->find();
                if (!empty($stash_last)) {
                    $stash_surplus_qty = $stash_last['stash_surplus_qty'] + $goods_qty[$key];
                } else {
                    $stash_surplus_qty = $stash_surplus_qty + $goods_qty[$key];
                }

                $stashGoodsDetails->save([
                    'order_no' => $params['order_no'],
                    'goods_id' => $item,
                    'be' => 1,
                    'qty' => $goods_qty[$key],
                    'stash_id' => $params['stash_id'],
                    'surplus_qty' => $goods->qty,
                    'stash_surplus_qty' => $stash_surplus_qty,
                    'price' => $goods_price[$key],
                    'amount' => bcmul($goods_price[$key], $goods_qty[$key], 2),
                    'cost_price' => $goods->cost_price,
                    'cost_amount' => bcmul($goods->cost_price, $goods_qty[$key], 2),
                    'remarks' => $remarks[$key] ?? '',
                    'status' => self::STATUS_JH,
                ]);
            }
        }


        if (!empty($params['amount'])) {
            $price = $params['amount'];
        } else {
//            if (empty($price)) {
//                $this->error = '错误的总计金额';
//                return false;
//            }
        }

        $this->save([
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'admin_id' => $params['admin_id'] ?? 0,
            'unit_id' => $params['unit_id'],
            'staff_id' => $params['staff_id'],
            'stash_id' => $params['stash_id'],
            'assets_id' => $params['assets_id'],
            'remarks' => $params['remarks'],
            'price' => $price,
            'status' => self::STATUS_JH,
        ]);


        $assetsModel = new Assets();
        $assets = $assetsModel->where('id', $params['assets_id'])->find();
        if (empty($assets)) {
            $this->error = '错误的账户';
            return false;
        }
        $assets->balance = bcsub($assets->balance, $price, 2);
        $assets->save();

        $assetsLogModel = new AssetsLog();
        $data = [
            'staff_id' => $params['staff_id'],
            'assets_id' => $params['assets_id'],
            'status' => self::STATUS_JH,
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'be' => 2,
            'price' => $price,
        ];
        $assetsLogModel->createLog($data);

        $financeModel = new Finance();
        $params = [
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'staff_id' => $params['staff_id'],
            'unit_id' => $params['unit_id'],
            'assets_id' => $params['assets_id'],
            'be' => 2,
            'price' => $price,
            'status' => Finance::STATUS_JH
        ];
        $financeModel->save($params);
        return true;

    }

    /**
     * Notes: 修改进货单
     * User: AerYYY
     * Date: 2/18/24 9:59 PM
     * @param $params
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit_xs_goods($params)
    {
        dump('暂时不修改');
        die;
        $old_order = $this->where('order_no', $params['order_no'])->find();
        $stashGoodsDetailsModel = new StashGoodsDetail();
        $old_list = $stashGoodsDetailsModel->where('order_no', $params['order_no'])->select();
        //退回所有库存
        foreach ($old_list as $value) {
            $goodsModel = new Goods();
            $goods = $goodsModel->where('id', $value['goods_id'])->find();
            if (!empty($goods)) {
                $goods->qty = $goods->qty - $value->qty;
                $goods->save();
            }
            $stashGoodsDetails = new StashGoodsDetail();
            $stash_surplus_qty = 0;
            $stash_last = $stashGoodsDetails->where(['goods_id' => $value['goods_id'], 'stash_id' => $params['stash_id']])->order('createtime desc')
                ->find();
            if (!empty($stash_last)) {
                $stash_last->stash_surplus_qty = $stash_last->stash_surplus_qty - $value['qty'];
            }

            $value->delete();
        }

        $goods_id = $params['goods_id'];
        $goods_qty = $params['goods_qty'];
        $goods_price = $params['goods_price'];
        $remarks = $params['remarks'];


    }


    /**
     * Notes: 进货退货单
     * User: AerYYY
     * Date: 2/5/24 11:19 PM
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function createJt($params)
    {
        $goods_id = $params['goods_id'];
        $goods_qty = $params['goods_qty'];
        $goods_price = $params['goods_price'];
        $remarks = $params['goods_remarks'];

        $price = 0;
        foreach ($goods_id as $key => $item) {
            if ($item && !empty($goods_qty[$key]) && $goods_qty[$key] > 0
                && !empty($goods_price[$key]) && $goods_price[$key] > 0) {
                $price += bcmul($goods_qty[$key], $goods_price[$key], 2);

                $goodsModel = new Goods();
                $goods = $goodsModel->where('id', $item)->find();
                if (empty($goods)) {
                    $this->error = '错误的商品数据';
                    return false;
                }
                $goods->qty = $goods->qty - $goods_qty[$key];
                $goods->save();

                $stashGoodsDetails = new StashGoodsDetail();
                $stash_surplus_qty = 0;
                $stash_last = $stashGoodsDetails->where(['goods_id' => $item, 'stash_id' => $params['stash_id']])->order('createtime desc')
                    ->find();
                if (!empty($stash_last)) {
                    $stash_surplus_qty = $stash_last['stash_surplus_qty'] - $goods_qty[$key];
                } else {
                    $stash_surplus_qty = $stash_surplus_qty - $goods_qty[$key];
                }

                $stashGoodsDetails->save([
                    'order_no' => $params['order_no'],
                    'goods_id' => $item,
                    'be' => 2,
                    'qty' => $goods_qty[$key],
                    'stash_id' => $params['stash_id'],
                    'stash_surplus_qty' => $stash_surplus_qty,
                    'surplus_qty' => $goods->qty,
                    'price' => $goods_price[$key],
                    'amount' => bcmul($goods_price[$key], $goods_qty[$key], 2),
                    'cost_price' => $goods->cost_price,
                    'cost_amount' => bcmul($goods->cost_price, $goods_qty[$key], 2),
                    'remarks' => $remarks[$key] ?? '',
                    'status' => self::STATUS_JHTH,
                ]);
            }
        }

        if (!empty($params['amount'])) {
            $price = $params['amount'];
        } else {
            if (empty($price)) {
                $this->error = '错误的总计金额';
                return false;
            }
        }
        $this->save([
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'admin_id' => $params['admin_id'] ?? 0,
            'unit_id' => $params['unit_id'],
            'staff_id' => $params['staff_id'],
            'stash_id' => $params['stash_id'],
            'assets_id' => $params['assets_id'],
            'remarks' => $params['remarks'],
            'price' => $price,
            'status' => self::STATUS_JHTH,
        ]);


        $assetsModel = new Assets();
        $assets = $assetsModel->where('id', $params['assets_id'])->find();
        if (empty($assets)) {
            $this->error = '错误的账户';
            return false;
        }
        $assets->balance = bcadd($assets->balance, $price, 2);
        $assets->save();

        $assetsLogModel = new AssetsLog();
        $data = [
            'staff_id' => $params['staff_id'],
            'assets_id' => $params['assets_id'],
            'status' => self::STATUS_JHTH,
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'be' => 1,
            'price' => $price,
        ];
        $assetsLogModel->createLog($data);


        $financeModel = new Finance();
        $params = [
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'staff_id' => $params['staff_id'],
            'unit_id' => $params['unit_id'],
            'assets_id' => $params['assets_id'],
            'be' => 1,
            'price' => $price,
            'status' => Finance::STATUS_JHTH
        ];
        $financeModel->save($params);

        return true;

    }

    /**
     * Notes: 销售单
     * User: AerYYY
     * Date: 2/5/24 11:19 PM
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function createXs($params)
    {
        $goods_id = $params['goods_id'];
        $goods_qty = $params['goods_qty'];
        $goods_price = $params['goods_price'];
        $remarks = $params['goods_remarks'];

        $price = 0;
        foreach ($goods_id as $key => $item) {
            if ($item && !empty($goods_qty[$key])) {

                $price += bcmul($goods_qty[$key], $goods_price[$key], 2);

                $goodsModel = new Goods();
                $goods = $goodsModel->where('id', $item)->find();
                if (empty($goods)) {
                    $this->error = '错误的商品数据';
                    return false;
                }
                $goods->qty = $goods->qty - $goods_qty[$key];
                $goods->save();

                $stashGoodsDetails = new StashGoodsDetail();
                $stash_surplus_qty = 0;
                $stash_last = $stashGoodsDetails->where(['goods_id' => $item, 'stash_id' => $params['stash_id']])->order('createtime desc')
                    ->find();
                if (!empty($stash_last)) {
                    $stash_surplus_qty = $stash_last['stash_surplus_qty'] - $goods_qty[$key];
                } else {
                    $stash_surplus_qty = $stash_surplus_qty - $goods_qty[$key];
                }

                $stashGoodsDetails->save([
                    'order_no' => $params['order_no'],
                    'goods_id' => $item,
                    'be' => 2,
                    'qty' => $goods_qty[$key],
                    'stash_id' => $params['stash_id'],
                    'surplus_qty' => $goods->qty,
                    'stash_surplus_qty' => $stash_surplus_qty,
                    'cost_price' => $goods->cost_price,
                    'cost_amount' => bcmul($goods->cost_price, $goods_qty[$key], 2),
                    'price' => $goods_price[$key] ?? 0,
                    'amount' => bcmul($goods_price[$key], $goods_qty[$key], 2) ?? 0,
                    'remarks' => $remarks[$key] ?? '',
                    'status' => self::STATUS_SH,
                ]);
            }
        }

        if (!empty($params['amount'])) {
            $price = $params['amount'];
        } else {
//            if (empty($price)) {
//                $this->error = '错误的总计金额';
//                return false;
//            }
        }
        $this->save([
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'admin_id' => $params['admin_id'] ?? 0,
            'unit_id' => $params['unit_id'],
            'staff_id' => $params['staff_id'] ?? 0,
            'stash_id' => $params['stash_id'],
            'assets_id' => $params['assets_id'],
            'remarks' => $params['remarks'],
            'price' => $price,
            'status' => self::STATUS_SH,
        ]);

        $assetsModel = new Assets();
        $assets = $assetsModel->where('id', $params['assets_id'])->find();
        if (empty($assets)) {
            $this->error = '错误的账户';
            return false;
        }
        $assets->balance = bcadd($assets->balance, $price, 2);
        $assets->save();

        $assetsLogModel = new AssetsLog();
        $data = [
            'staff_id' => $params['staff_id'] ?? 0,
            'assets_id' => $params['assets_id'],
            'status' => self::STATUS_SH,
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'be' => 1,
            'price' => $price,
        ];
        $assetsLogModel->createLog($data);

        $financeModel = new Finance();
        $params = [
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'staff_id' => $params['staff_id'] ?? 0,
            'unit_id' => $params['unit_id'],
            'assets_id' => $params['assets_id'],
            'be' => 1,
            'price' => $price,
            'status' => Finance::STATUS_SH
        ];
        $financeModel->save($params);

        return true;

    }

    /**
     * Notes: 修改销售单
     * User: AerYYY
     * Date: 5/14/24 10:35 AM
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editXs($params)
    {
        $old_list = StashGoodsDetail::where('order_no', $params['order_no'])->select();
        $old_list = collection($old_list)->toArray();
        $old_list = array_column($old_list, null, 'goods_id');

        $old_amount = 0;
        //库存还原
        foreach ($old_list as $item) {
            $goodsModel = new Goods();
            $goods = $goodsModel->where('id', $item['goods_id'])->find();
            if (!empty($goods)) {
                $goods->qty = $goods->qty + $item['qty'];
                $goods->save();
            }
            $old_amount += $item['amount'];
        }
        //删除以前的日志
        StashGoodsDetail::where('order_no', $params['order_no'])->delete();

        $goods_id = $params['goods_id'];
        $goods_qty = $params['goods_qty'];
        $goods_price = $params['goods_price'];
        $remarks = $params['goods_remarks'];

        $price = 0;
        foreach ($goods_id as $key => $item) {
            if ($item && !empty($goods_qty[$key]) && $goods_qty[$key] > 0) {
                $price += bcmul($goods_qty[$key], $goods_price[$key], 2);

                $goodsModel = new Goods();
                $goods = $goodsModel->where('id', $item)->find();
                if (empty($goods)) {
                    $this->error = '错误的商品数据';
                    return false;
                }
                $goods->qty = $goods->qty - $goods_qty[$key];
                $goods->save();

                $stashGoodsDetails = new StashGoodsDetail();
                $stash_surplus_qty = 0;
                $stash_last = $stashGoodsDetails->where(['goods_id' => $item, 'stash_id' => $params['stash_id']])->order('createtime desc')
                    ->find();
                if (!empty($stash_last)) {
                    $stash_surplus_qty = $stash_last['stash_surplus_qty'] - $goods_qty[$key];
                } else {
                    $stash_surplus_qty = $stash_surplus_qty - $goods_qty[$key];
                }

                $stashGoodsDetails->save([
                    'order_no' => $params['order_no'],
                    'goods_id' => $item,
                    'be' => 2,
                    'qty' => $goods_qty[$key],
                    'stash_id' => $params['stash_id'],
                    'surplus_qty' => $goods->qty,
                    'stash_surplus_qty' => $stash_surplus_qty,
                    'cost_price' => $goods->cost_price,
                    'cost_amount' => bcmul($goods->cost_price, $goods_qty[$key], 2),
                    'price' => $goods_price[$key] ?? 0,
                    'amount' => bcmul($goods_price[$key], $goods_qty[$key], 2) ?? 0,
                    'remarks' => $remarks[$key] ?? '',
                    'status' => self::STATUS_SH,
                ]);
            }
        }

        $assetsLogModel = new AssetsLog();
        $info = $assetsLogModel->where('order_no', $params['order_no'])->find();
        if (!empty($info) && $info['price'] != $price) {
            $assetsModel = new Assets();
            $assets = $assetsModel->where('id', $params['assets_id'])->find();
            if (empty($assets)) {
                $this->error = '错误的账户';
                return false;
            }
            if ($old_amount > $price) {
                $be = 2;
                $money = round($old_amount - $price, 2);
                $assets->balance = bcsub($assets->balance, $money, 2);
            } else {
                $be = 1;
                $money = round($price - $old_amount, 2);
                $assets->balance = bcadd($assets->balance, $money, 2);
            }
            $assets->save();
            $assetsLogModel = new AssetsLog();
            $data = [
                'staff_id' => $params['staff_id'] ?? 0,
                'assets_id' => $params['assets_id'],
                'status' => Finance::STATUS_SYSTEM,
                'order_no' => $params['order_no'],
                'date' => $params['date'],
                'be' => $be,
                'price' => $money,
            ];
            $assetsLogModel->createLog($data);
        }

        $info = $this->where('order_no', $params['order_no'])->find();
        if (!empty($info)) {
            $info->save([
                'order_no' => $params['order_no'],
                'date' => $params['date'],
                'admin_id' => $params['admin_id'] ?? 0,
                'unit_id' => $params['unit_id'],
                'staff_id' => $params['staff_id'] ?? 0,
                'stash_id' => $params['stash_id'],
                'assets_id' => $params['assets_id'],
                'remarks' => $params['remarks'],
                'price' => $price,
                'status' => self::STATUS_SH,
            ]);
        }

        $financeModel = new Finance();
        $finance = $financeModel->where('order_no', $params['order_no'])->find();
        if (!empty($finance)) {
            $finance->save([
                'staff_id' => $params['staff_id'] ?? 0,
                'unit_id' => $params['unit_id'],
                'assets_id' => $params['assets_id'],
                'be' => 1,
                'price' => $price,
            ]);
        }

        return true;
    }

    /**
     * Notes: 修改销售退货单
     * User: AerYYY
     * Date: 5/14/24 10:58 AM
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editXsthd($params)
    {
        $old_list = StashGoodsDetail::where('order_no', $params['order_no'])->select();
        $old_list = collection($old_list)->toArray();
        $old_list = array_column($old_list, null, 'goods_id');

        $old_amount = 0;
        //库存还原
        foreach ($old_list as $item) {
            $goodsModel = new Goods();
            $goods = $goodsModel->where('id', $item['goods_id'])->find();
            if (!empty($goods)) {
                $goods->qty = $goods->qty - $item['qty'];
                $goods->save();
            }
            $old_amount += $item['amount'];
        }

        //删除以前的日志
        StashGoodsDetail::where('order_no', $params['order_no'])->delete();

        $goods_id = $params['goods_id'];
        $goods_qty = $params['goods_qty'];
        $goods_price = $params['goods_price'];
        $remarks = $params['goods_remarks'];

        $price = 0;
        foreach ($goods_id as $key => $item) {
            if ($item && !empty($goods_qty[$key]) && $goods_qty[$key] > 0) {
                $price += bcmul($goods_qty[$key], $goods_price[$key], 2);

                $goodsModel = new Goods();
                $goods = $goodsModel->where('id', $item)->find();
                if (empty($goods)) {
                    $this->error = '错误的商品数据';
                    return false;
                }
                $goods->qty = $goods->qty + $goods_qty[$key];
                $goods->save();

                $stashGoodsDetails = new StashGoodsDetail();
                $stash_surplus_qty = 0;
                $stash_last = $stashGoodsDetails->where(['goods_id' => $item, 'stash_id' => $params['stash_id']])->order('createtime desc')
                    ->find();
                if (!empty($stash_last)) {
                    $stash_surplus_qty = $stash_last['stash_surplus_qty'] + $goods_qty[$key];
                } else {
                    $stash_surplus_qty = $stash_surplus_qty + $goods_qty[$key];
                }

                $stashGoodsDetails->save([
                    'order_no' => $params['order_no'],
                    'goods_id' => $item,
                    'be' => 1,
                    'qty' => $goods_qty[$key],
                    'stash_id' => $params['stash_id'],
                    'surplus_qty' => $goods->qty,
                    'stash_surplus_qty' => $stash_surplus_qty,
                    'cost_price' => $goods->cost_price,
                    'cost_amount' => bcmul($goods->cost_price, $goods_qty[$key], 2),
                    'price' => $goods_price[$key] ?? 0,
                    'amount' => bcmul($goods_price[$key], $goods_qty[$key], 2) ?? 0,
                    'remarks' => $remarks[$key] ?? '',
                    'status' => self::STATUS_SHTH,
                ]);
            }
        }

        $assetsLogModel = new AssetsLog();
        $info = $assetsLogModel->where('order_no', $params['order_no'])->find();
        if (!empty($info) && $info['price'] != $price) {
            $assetsModel = new Assets();
            $assets = $assetsModel->where('id', $params['assets_id'])->find();
            if (empty($assets)) {
                $this->error = '错误的账户';
                return false;
            }
            if ($old_amount > $price) {
                $be = 1;
                $money = round($old_amount - $price, 2);
                $assets->balance = bcadd($assets->balance, $money, 2);
            } else {
                $be = 2;
                $money = round($price - $old_amount, 2);
                $assets->balance = bcsub($assets->balance, $money, 2);
            }
            $assets->save();
            $assetsLogModel = new AssetsLog();
            $data = [
                'staff_id' => $params['staff_id'] ?? 0,
                'assets_id' => $params['assets_id'],
                'status' => Finance::STATUS_SYSTEM,
                'order_no' => $params['order_no'],
                'date' => $params['date'],
                'be' => $be,
                'price' => $money,
            ];
            $assetsLogModel->createLog($data);
        }

        $info = $this->where('order_no', $params['order_no'])->find();
        if (!empty($info)) {
            $info->save([
                'order_no' => $params['order_no'],
                'date' => $params['date'],
                'admin_id' => $params['admin_id'] ?? 0,
                'unit_id' => $params['unit_id'],
                'staff_id' => $params['staff_id'] ?? 0,
                'stash_id' => $params['stash_id'],
                'assets_id' => $params['assets_id'],
                'remarks' => $params['remarks'],
                'price' => $price,
                'status' => self::STATUS_SHTH,
            ]);
        }

        $financeModel = new Finance();
        $finance = $financeModel->where('order_no', $params['order_no'])->find();
        if (!empty($finance)) {
            $finance->save([
                'staff_id' => $params['staff_id'] ?? 0,
                'unit_id' => $params['unit_id'],
                'assets_id' => $params['assets_id'],
                'be' => 2,
                'price' => $price,
            ]);
        }

        return true;

    }

    /**
     * Notes: 销售退货单
     * User: AerYYY
     * Date: 2/5/24 11:27 PM
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function createXt($params)
    {
        $goods_id = $params['goods_id'];
        $goods_qty = $params['goods_qty'];
        $goods_price = $params['goods_price'];
        $remarks = $params['goods_remarks'];

        $price = 0;
        foreach ($goods_id as $key => $item) {
            if ($item && !empty($goods_qty[$key]) && $goods_qty[$key] > 0) {

                $price += bcmul($goods_qty[$key], $goods_price[$key], 2);

                $goodsModel = new Goods();
                $goods = $goodsModel->where('id', $item)->find();
                if (empty($goods)) {
                    $this->error = '错误的商品数据';
                    return false;
                }
                $goods->qty = $goods->qty + $goods_qty[$key];
                $goods->save();

                $stashGoodsDetails = new StashGoodsDetail();
                $stash_surplus_qty = 0;
                $stash_last = $stashGoodsDetails->where(['goods_id' => $item, 'stash_id' => $params['stash_id']])->order('createtime desc')
                    ->find();
                if (!empty($stash_last)) {
                    $stash_surplus_qty = $stash_last['stash_surplus_qty'] + $goods_qty[$key];
                } else {
                    $stash_surplus_qty = $stash_surplus_qty + $goods_qty[$key];
                }
                $stashGoodsDetails->save([
                    'order_no' => $params['order_no'],
                    'goods_id' => $item,
                    'be' => 1,
                    'qty' => $goods_qty[$key],
                    'stash_id' => $params['stash_id'],
                    'surplus_qty' => $goods->qty,
                    'stash_surplus_qty' => $stash_surplus_qty,
                    'cost_price' => $goods->cost_price,
                    'cost_amount' => bcmul($goods->cost_price, $goods_qty[$key], 2),
                    'price' => $goods_price[$key] ?? 0,
                    'amount' => bcmul($goods_price[$key], $goods_qty[$key], 2) ?? 0,
                    'remarks' => $remarks[$key] ?? '',
                    'status' => self::STATUS_SHTH,
                ]);
            }
        }

        if (!empty($params['amount'])) {
            $price = $params['amount'];
        } else {
//            if (empty($price)) {
//                $this->error = '错误的总计金额';
//                return false;
//            }
        }
        $this->save([
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'admin_id' => $params['admin_id'] ?? 0,
            'unit_id' => $params['unit_id'],
            'staff_id' => $params['staff_id'],
            'stash_id' => $params['stash_id'],
            'assets_id' => $params['assets_id'],
            'remarks' => $params['remarks'],
            'price' => $price,
            'status' => self::STATUS_SHTH,
        ]);


        $assetsModel = new Assets();
        $assets = $assetsModel->where('id', $params['assets_id'])->find();
        if (empty($assets)) {
            $this->error = '错误的账户';
            return false;
        }
        $assets->balance = bcsub($assets->balance, $price, 2);
        $assets->save();

        $assetsLogModel = new AssetsLog();
        $data = [
            'staff_id' => $params['staff_id'],
            'assets_id' => $params['assets_id'],
            'status' => self::STATUS_SHTH,
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'be' => 2,
            'price' => $price,
        ];
        $assetsLogModel->createLog($data);

        $financeModel = new Finance();
        $params = [
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'staff_id' => $params['staff_id'],
            'unit_id' => $params['unit_id'],
            'assets_id' => $params['assets_id'],
            'be' => 2,
            'price' => $price,
            'status' => Finance::STATUS_SHTH
        ];
        $financeModel->save($params);

        return true;

    }


    /**
     * Notes:  调拨单
     * User: AerYYY
     * Date: 2/16/24 2:54 PM
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function allotCreate($params): bool
    {
        $goods_id = $params['goods_id'];
        $goods_qty = $params['goods_qty'];
        $goods_price = $params['goods_price'];
        $remarks = $params['remarks'];
        if ($params['stash_id'] == $params['stash_c_id']) {
            $this->error = '出货仓库和收货仓库不能是同一个仓库';
            return false;
        }
        $price = 0;
        foreach ($goods_id as $key => $item) {
            if ($item && !empty($goods_qty[$key]) && $goods_qty[$key] > 0) {

                $price += bcmul($goods_qty[$key], $goods_price[$key], 2);
                $goodsModel = new Goods();
                $goods = $goodsModel->where('id', $item)->find();
                if (empty($goods)) {
                    $this->error = '错误的商品数据';
                    return false;
                }
                $stash_surplus_qty = 0;
                $stashGoodsDetails = new StashGoodsDetail();
                $stash_last = $stashGoodsDetails->where(['goods_id' => $item, 'stash_id' => $params['stash_id']])->order('createtime desc')
                    ->find();
                if (!empty($stash_last)) {
                    $stash_surplus_qty = $stash_last['stash_surplus_qty'] - $goods_qty[$key];
                } else {
                    $stash_surplus_qty = $stash_surplus_qty - $goods_qty[$key];
                }
                $stashGoodsDetails->save([
                    'order_no' => $params['order_no'],
                    'goods_id' => $item,
                    'be' => 2,
                    'qty' => $goods_qty[$key],
                    'stash_id' => $params['stash_id'],
                    'surplus_qty' => $goods->qty,
                    'stash_surplus_qty' => $stash_surplus_qty,
                    'price' => $goods_price[$key] ?? 0,
                    'remarks' => $remarks[$key] ?? '',
                    'status' => Finance::STATUS_DBD_C,
                ]);

                $stash_surplus_qty = 0;
                $stashGoodsDetails = new StashGoodsDetail();
                $stash_last = $stashGoodsDetails->where(['goods_id' => $item, 'stash_id' => $params['stash_c_id']])->order('createtime desc')
                    ->find();
                if (!empty($stash_last)) {
                    $stash_surplus_qty = $stash_last['stash_surplus_qty'] + $goods_qty[$key];
                } else {
                    $stash_surplus_qty = $stash_surplus_qty + $goods_qty[$key];
                }
                $stashGoodsDetails->save([
                    'order_no' => $params['order_no'],
                    'goods_id' => $item,
                    'be' => 1,
                    'qty' => $goods_qty[$key],
                    'stash_id' => $params['stash_c_id'],
                    'surplus_qty' => $goods->qty,
                    'stash_surplus_qty' => $stash_surplus_qty,
                    'price' => $goods_price[$key] ?? 0,
                    'remarks' => $remarks[$key] ?? '',
                    'status' => Finance::STATUS_DBD_R,
                ]);

            }
        }

        $this->save([
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'admin_id' => $params['admin_id'] ?? 0,
            'staff_id' => $params['staff_id'],
            'stash_c_id' => $params['stash_c_id'],
            'stash_id' => $params['stash_id'],
            'remarks' => $params['remarks'],
            'price' => $price,
            'status' => Finance::STATUS_DBD,
        ]);

        $financeModel = new Finance();
        $params = [
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'staff_id' => $params['staff_id'],
            'be' => 0,
            'status' => Finance::STATUS_DBD
        ];
        $financeModel->save($params);
        return true;

    }

    /**
     * Notes: 其他入库单
     * User: AerYYY
     * Date: 2/16/24 9:55 PM
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function other_warehousing($params)
    {
        $goods_id = $params['goods_id'];
        $goods_qty = $params['goods_qty'];
        $goods_price = $params['goods_price'];
        $remarks = $params['goods_remarks'];

        $price = 0;
        foreach ($goods_id as $key => $item) {
            if ($item && !empty($goods_qty[$key]) && $goods_qty[$key] > 0) {
                $price += bcmul($goods_qty[$key], $goods_price[$key], 2);
                $goodsModel = new Goods();
                $goods = $goodsModel->where('id', $item)->find();
                if (empty($goods)) {
                    $this->error = '错误的商品数据';
                    return false;
                }
                $goods->qty = $goods->qty + $goods_qty[$key];
                $goods->save();
                $stash_surplus_qty = 0;
                $stashGoodsDetails = new StashGoodsDetail();
                $stash_last = $stashGoodsDetails->where(['goods_id' => $item, 'stash_id' => $params['stash_id']])
                    ->order('createtime desc')
                    ->find();
                if (!empty($stash_last)) {
                    $stash_surplus_qty = $stash_last['stash_surplus_qty'] + $goods_qty[$key];
                } else {
                    $stash_surplus_qty = $stash_surplus_qty + $goods_qty[$key];
                }
                $stashGoodsDetails->save([
                    'order_no' => $params['order_no'],
                    'goods_id' => $item,
                    'be' => 1,
                    'qty' => $goods_qty[$key],
                    'stash_id' => $params['stash_id'],
                    'surplus_qty' => $goods->qty,
                    'stash_surplus_qty' => $stash_surplus_qty,
                    'price' => $goods_price[$key] ?? 0,
                    'amount' => round($goods_price[$key] * $goods_qty[$key], 2) ?? 0,
                    'cost_amount' => round($goods_qty[$key] * $goods->cost_price, 2),
                    'category_id' => $params['category_id'],
                    'remarks' => $remarks[$key] ?? '',
                    'status' => Finance::STATUS_QTR,
                ]);


            }
        }


        $this->save([
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'admin_id' => $params['admin_id'] ?? 0,
            'staff_id' => $params['staff_id'],
            'unit_id' => $params['unit_id'],
            'stash_id' => $params['stash_id'],
            'category_id' => $params['category_id'],
            'remarks' => $params['remarks'],
            'price' => $price,
            'status' => Finance::STATUS_QTR,
        ]);


        $financeModel = new Finance();
        $params = [
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'staff_id' => $params['staff_id'],
            'unit_id' => $params['unit_id'],
            'be' => 0,
            'status' => Finance::STATUS_QTR
        ];
        $financeModel->save($params);
        return true;
    }

    /**
     * Notes: 其他出库单
     * User: AerYYY
     * Date: 2/16/24 9:55 PM
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function other_outbound($params)
    {
        $goods_id = $params['goods_id'];
        $goods_qty = $params['goods_qty'];
        $goods_price = $params['goods_price'];
        $remarks = $params['goods_remarks'];
        $price = 0;
        foreach ($goods_id as $key => $item) {
            if ($item && !empty($goods_qty[$key]) && $goods_qty[$key] > 0) {
                $price += bcmul($goods_qty[$key], $goods_price[$key], 2);
                $goodsModel = new Goods();
                $goods = $goodsModel->where('id', $item)->find();
                if (empty($goods)) {
                    $this->error = '错误的商品数据';
                    return false;
                }
                $goods->qty = $goods->qty - $goods_qty[$key];
                $goods->save();
                $stash_surplus_qty = 0;
                $stashGoodsDetails = new StashGoodsDetail();
                $stash_last = $stashGoodsDetails->where(['goods_id' => $item, 'stash_id' => $params['stash_id']])->order('createtime desc')
                    ->find();
                if (!empty($stash_last)) {
                    $stash_surplus_qty = $stash_last['stash_surplus_qty'] - $goods_qty[$key];
                } else {
                    $stash_surplus_qty = $stash_surplus_qty - $goods_qty[$key];
                }
                $stashGoodsDetails->save([
                    'order_no' => $params['order_no'],
                    'goods_id' => $item,
                    'be' => 2,
                    'qty' => $goods_qty[$key],
                    'stash_id' => $params['stash_id'],
                    'surplus_qty' => $goods->qty,
                    'stash_surplus_qty' => $stash_surplus_qty,
                    'price' => $goods_price[$key] ?? 0,
                    'amount' => round($goods_price[$key] * $goods_qty[$key], 2) ?? 0,
                    'cost_amount' => round($goods_qty[$key] * $goods->cost_price, 2),
                    'category_id' => $params['category_id'],
                    'remarks' => $remarks[$key] ?? '',
                    'status' => Finance::STATUS_QTC,
                ]);


            }
        }

        $this->save([
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'admin_id' => $params['admin_id'] ?? 0,
            'staff_id' => $params['staff_id'],
            'unit_id' => $params['unit_id'],
            'stash_id' => $params['stash_id'],
            'category_id' => $params['category_id'],
            'remarks' => $params['remarks'],
            'price' => $price,
            'status' => Finance::STATUS_QTC,
        ]);


        $financeModel = new Finance();
        $params = [
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'staff_id' => $params['staff_id'],
            'unit_id' => $params['unit_id'],
            'be' => 0,
            'status' => Finance::STATUS_QTC
        ];
        $financeModel->save($params);
        return true;
    }

    /**
     * Notes: 修改进货单
     * User: AerYYY
     * Date: 5/16/24 4:58 PM
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editJhd($params)
    {

        $old_list = StashGoodsDetail::where('order_no', $params['order_no'])->select();
        $old_list = collection($old_list)->toArray();
        $old_list = array_column($old_list, null, 'goods_id');

        $old_amount = 0;
        //库存还原
        foreach ($old_list as $item) {
            $goodsModel = new Goods();
            $goods = $goodsModel->where('id', $item['goods_id'])->find();
            if (!empty($goods)) {
                $goods->qty = $goods->qty - $item['qty'];
                $goods->save();
            }
            $old_amount += $item['amount'];
        }

        //删除以前的日志
        StashGoodsDetail::where('order_no', $params['order_no'])->delete();

        $goods_id = $params['goods_id'];
        $goods_qty = $params['goods_qty'];
        $goods_price = $params['goods_price'];
        $remarks = $params['goods_remarks'];

        $price = 0;
        foreach ($goods_id as $key => $item) {
            if ($item && !empty($goods_qty[$key]) && $goods_qty[$key] > 0) {
                $price += bcmul($goods_qty[$key], $goods_price[$key], 2);

                $goodsModel = new Goods();
                $goods = $goodsModel->where('id', $item)->find();
                if (empty($goods)) {
                    $this->error = '错误的商品数据';
                    return false;
                }
                $goods->qty = $goods->qty + $goods_qty[$key];
                $goods->save();

                $stashGoodsDetails = new StashGoodsDetail();
                $stash_surplus_qty = 0;
                $stash_last = $stashGoodsDetails->where(['goods_id' => $item, 'stash_id' => $params['stash_id']])->order('createtime desc')
                    ->find();
                if (!empty($stash_last)) {
                    $stash_surplus_qty = $stash_last['stash_surplus_qty'] + $goods_qty[$key];
                } else {
                    $stash_surplus_qty = $stash_surplus_qty + $goods_qty[$key];
                }

                $stashGoodsDetails->save([
                    'order_no' => $params['order_no'],
                    'goods_id' => $item,
                    'be' => 1,
                    'qty' => $goods_qty[$key],
                    'stash_id' => $params['stash_id'],
                    'surplus_qty' => $goods->qty,
                    'stash_surplus_qty' => $stash_surplus_qty,
                    'cost_price' => $goods->cost_price,
                    'cost_amount' => bcmul($goods->cost_price, $goods_qty[$key], 2),
                    'price' => $goods_price[$key] ?? 0,
                    'amount' => bcmul($goods_price[$key], $goods_qty[$key], 2) ?? 0,
                    'remarks' => $remarks[$key] ?? '',
                    'status' => self::STATUS_JH,
                ]);
            }
        }

        $assetsLogModel = new AssetsLog();
        $info = $assetsLogModel->where('order_no', $params['order_no'])->find();
        if (!empty($info) && $info['price'] != $price) {
            $assetsModel = new Assets();
            $assets = $assetsModel->where('id', $params['assets_id'])->find();
            if (empty($assets)) {
                $this->error = '错误的账户';
                return false;
            }
            if ($old_amount > $price) {
                $be = 1;
                $money = round($old_amount - $price, 2);
                $assets->balance = bcadd($assets->balance, $money, 2);
            } else {
                $be = 2;
                $money = round($price - $old_amount, 2);
                $assets->balance = bcsub($assets->balance, $money, 2);
            }
            $assets->save();
            $assetsLogModel = new AssetsLog();
            $data = [
                'staff_id' => $params['staff_id'] ?? 0,
                'assets_id' => $params['assets_id'],
                'status' => Finance::STATUS_SYSTEM,
                'order_no' => $params['order_no'],
                'date' => $params['date'],
                'be' => $be,
                'price' => $money,
            ];
            $assetsLogModel->createLog($data);
        }

        $info = $this->where('order_no', $params['order_no'])->find();
        if (!empty($info)) {
            $info->save([
                'order_no' => $params['order_no'],
                'date' => $params['date'],
                'admin_id' => $params['admin_id'] ?? 0,
                'unit_id' => $params['unit_id'],
                'staff_id' => $params['staff_id'] ?? 0,
                'stash_id' => $params['stash_id'],
                'assets_id' => $params['assets_id'],
                'remarks' => $params['remarks'],
                'price' => $price,
                'status' => self::STATUS_JH,
            ]);
        }

        $financeModel = new Finance();
        $finance = $financeModel->where('order_no', $params['order_no'])->find();
        if (!empty($finance)) {
            $finance->save([
                'staff_id' => $params['staff_id'] ?? 0,
                'unit_id' => $params['unit_id'],
                'assets_id' => $params['assets_id'],
                'be' => 2,
                'price' => $price,
            ]);
        }

        return true;
    }

    /**
     * Notes: 修改进货退货单
     * User: AerYYY
     * Date: 5/17/24 2:48 PM
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editThd($params)
    {
        $old_list = StashGoodsDetail::where('order_no', $params['order_no'])->select();
        $old_list = collection($old_list)->toArray();
        $old_list = array_column($old_list, null, 'goods_id');

        $old_amount = 0;
        //库存还原
        foreach ($old_list as $item) {
            $goodsModel = new Goods();
            $goods = $goodsModel->where('id', $item['goods_id'])->find();
            if (!empty($goods)) {
                $goods->qty = $goods->qty + $item['qty'];
                $goods->save();
            }
            $old_amount += $item['amount'];
        }
        //删除以前的日志
        StashGoodsDetail::where('order_no', $params['order_no'])->delete();

        $goods_id = $params['goods_id'];
        $goods_qty = $params['goods_qty'];
        $goods_price = $params['goods_price'];
        $remarks = $params['goods_remarks'];

        $price = 0;
        foreach ($goods_id as $key => $item) {
            if ($item && !empty($goods_qty[$key]) && $goods_qty[$key] > 0) {
                $price += bcmul($goods_qty[$key], $goods_price[$key], 2);

                $goodsModel = new Goods();
                $goods = $goodsModel->where('id', $item)->find();
                if (empty($goods)) {
                    $this->error = '错误的商品数据';
                    return false;
                }
                $goods->qty = $goods->qty - $goods_qty[$key];
                $goods->save();

                $stashGoodsDetails = new StashGoodsDetail();
                $stash_surplus_qty = 0;
                $stash_last = $stashGoodsDetails->where(['goods_id' => $item, 'stash_id' => $params['stash_id']])->order('createtime desc')
                    ->find();
                if (!empty($stash_last)) {
                    $stash_surplus_qty = $stash_last['stash_surplus_qty'] - $goods_qty[$key];
                } else {
                    $stash_surplus_qty = $stash_surplus_qty - $goods_qty[$key];
                }

                $stashGoodsDetails->save([
                    'order_no' => $params['order_no'],
                    'goods_id' => $item,
                    'be' => 2,
                    'qty' => $goods_qty[$key],
                    'stash_id' => $params['stash_id'],
                    'surplus_qty' => $goods->qty,
                    'stash_surplus_qty' => $stash_surplus_qty,
                    'cost_price' => $goods->cost_price,
                    'cost_amount' => bcmul($goods->cost_price, $goods_qty[$key], 2),
                    'price' => $goods_price[$key] ?? 0,
                    'amount' => bcmul($goods_price[$key], $goods_qty[$key], 2) ?? 0,
                    'remarks' => $remarks[$key] ?? '',
                    'status' => self::STATUS_SH,
                ]);
            }
        }

        $assetsLogModel = new AssetsLog();
        $info = $assetsLogModel->where('order_no', $params['order_no'])->find();
        if (!empty($info) && $info['price'] != $price) {
            $assetsModel = new Assets();
            $assets = $assetsModel->where('id', $params['assets_id'])->find();
            if (empty($assets)) {
                $this->error = '错误的账户';
                return false;
            }
            if ($old_amount > $price) {
                $be = 2;
                $money = round($old_amount - $price, 2);
                $assets->balance = bcsub($assets->balance, $money, 2);
            } else {
                $be = 1;
                $money = round($price - $old_amount, 2);
                $assets->balance = bcadd($assets->balance, $money, 2);
            }
            $assets->save();
            $assetsLogModel = new AssetsLog();
            $data = [
                'staff_id' => $params['staff_id'] ?? 0,
                'assets_id' => $params['assets_id'],
                'status' => Finance::STATUS_SYSTEM,
                'order_no' => $params['order_no'],
                'date' => $params['date'],
                'be' => $be,
                'price' => $money,
            ];
            $assetsLogModel->createLog($data);
        }

        $info = $this->where('order_no', $params['order_no'])->find();
        if (!empty($info)) {
            $info->save([
                'order_no' => $params['order_no'],
                'date' => $params['date'],
                'admin_id' => $params['admin_id'] ?? 0,
                'unit_id' => $params['unit_id'],
                'staff_id' => $params['staff_id'] ?? 0,
                'stash_id' => $params['stash_id'],
                'assets_id' => $params['assets_id'],
                'remarks' => $params['remarks'],
                'price' => $price,
                'status' => Finance::STATUS_JHTH,
            ]);
        }

        $financeModel = new Finance();
        $finance = $financeModel->where('order_no', $params['order_no'])->find();
        if (!empty($finance)) {
            $finance->save([
                'staff_id' => $params['staff_id'] ?? 0,
                'unit_id' => $params['unit_id'],
                'assets_id' => $params['assets_id'],
                'be' => 1,
                'price' => $price,
            ]);
        }

        return true;
    }

    public function editDbd()
    {
        return false;
    }

    /**
     * Notes: 修改其他入库单
     * User: AerYYY
     * Date: 5/17/24 3:43 PM
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editQtrkd($params)
    {
        $old_list = StashGoodsDetail::where('order_no', $params['order_no'])->select();
        $old_list = collection($old_list)->toArray();
        $old_list = array_column($old_list, null, 'goods_id');

        $old_amount = 0;
        //库存还原
        foreach ($old_list as $item) {
            $goodsModel = new Goods();
            $goods = $goodsModel->where('id', $item['goods_id'])->find();
            if (!empty($goods)) {
                $goods->qty = $goods->qty - $item['qty'];
                $goods->save();
            }
            $old_amount += $item['amount'];
        }

        //删除以前的日志
        StashGoodsDetail::where('order_no', $params['order_no'])->delete();

        $goods_id = $params['goods_id'];
        $goods_qty = $params['goods_qty'];
        $goods_price = $params['goods_price'];
        $remarks = $params['goods_remarks'];

        $price = 0;
        foreach ($goods_id as $key => $item) {
            if ($item && !empty($goods_qty[$key]) && $goods_qty[$key] > 0) {
                $price += bcmul($goods_qty[$key], $goods_price[$key], 2);

                $goodsModel = new Goods();
                $goods = $goodsModel->where('id', $item)->find();
                if (empty($goods)) {
                    $this->error = '错误的商品数据';
                    return false;
                }
                $goods->qty = $goods->qty + $goods_qty[$key];
                $goods->save();

                $stashGoodsDetails = new StashGoodsDetail();
                $stash_surplus_qty = 0;
                $stash_last = $stashGoodsDetails->where(['goods_id' => $item, 'stash_id' => $params['stash_id']])->order('createtime desc')
                    ->find();
                if (!empty($stash_last)) {
                    $stash_surplus_qty = $stash_last['stash_surplus_qty'] + $goods_qty[$key];
                } else {
                    $stash_surplus_qty = $stash_surplus_qty + $goods_qty[$key];
                }

                $stashGoodsDetails->save([
                    'order_no' => $params['order_no'],
                    'goods_id' => $item,
                    'be' => 1,
                    'qty' => $goods_qty[$key],
                    'stash_id' => $params['stash_id'],
                    'surplus_qty' => $goods->qty,
                    'stash_surplus_qty' => $stash_surplus_qty,
                    'cost_price' => $goods->cost_price,
                    'cost_amount' => bcmul($goods->cost_price, $goods_qty[$key], 2),
                    'price' => $goods_price[$key] ?? 0,
                    'amount' => bcmul($goods_price[$key], $goods_qty[$key], 2) ?? 0,
                    'remarks' => $remarks[$key] ?? '',
                    'status' => Finance::STATUS_QTR,
                ]);
            }
        }

        $info = $this->where('order_no', $params['order_no'])->find();
        if (!empty($info)) {
            $info->save([
                'order_no' => $params['order_no'],
                'date' => $params['date'],
                'admin_id' => $params['admin_id'] ?? 0,
                'unit_id' => $params['unit_id'],
                'staff_id' => $params['staff_id'] ?? 0,
                'stash_id' => $params['stash_id'],
                'remarks' => $params['remarks'],
                'price' => $price,
                'status' => Finance::STATUS_QTR,
            ]);
        }

        $financeModel = new Finance();
        $finance = $financeModel->where('order_no', $params['order_no'])->find();
        if (!empty($finance)) {
            $finance->save([
                'staff_id' => $params['staff_id'] ?? 0,
                'unit_id' => $params['unit_id'],
                'be' => 2,
                'price' => $price,
            ]);
        }

        return true;

    }


    /**
     * Notes: 修改其他出库单
     * User: AerYYY
     * Date: 5/17/24 4:00 PM
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editQtckd($params)
    {
        $old_list = StashGoodsDetail::where('order_no', $params['order_no'])->select();
        $old_list = collection($old_list)->toArray();
        $old_list = array_column($old_list, null, 'goods_id');

        $old_amount = 0;
        //库存还原
        foreach ($old_list as $item) {
            $goodsModel = new Goods();
            $goods = $goodsModel->where('id', $item['goods_id'])->find();
            if (!empty($goods)) {
                $goods->qty = $goods->qty + $item['qty'];
                $goods->save();
            }
            $old_amount += $item['amount'];
        }

        //删除以前的日志
        StashGoodsDetail::where('order_no', $params['order_no'])->delete();

        $goods_id = $params['goods_id'];
        $goods_qty = $params['goods_qty'];
        $goods_price = $params['goods_price'];
        $remarks = $params['goods_remarks'];

        $price = 0;
        foreach ($goods_id as $key => $item) {
            if ($item && !empty($goods_qty[$key]) && $goods_qty[$key] > 0) {

                $price += bcmul($goods_qty[$key], $goods_price[$key], 2);

                $goodsModel = new Goods();
                $goods = $goodsModel->where('id', $item)->find();
                if (empty($goods)) {
                    $this->error = '错误的商品数据';
                    return false;
                }
                $goods->qty = $goods->qty - $goods_qty[$key];
                $goods->save();

                $stashGoodsDetails = new StashGoodsDetail();
                $stash_surplus_qty = 0;
                $stash_last = $stashGoodsDetails->where(['goods_id' => $item, 'stash_id' => $params['stash_id']])->order('createtime desc')
                    ->find();
                if (!empty($stash_last)) {
                    $stash_surplus_qty = $stash_last['stash_surplus_qty'] + $goods_qty[$key];
                } else {
                    $stash_surplus_qty = $stash_surplus_qty + $goods_qty[$key];
                }

                $stashGoodsDetails->save([
                    'order_no' => $params['order_no'],
                    'goods_id' => $item,
                    'be' => 2,
                    'qty' => $goods_qty[$key],
                    'stash_id' => $params['stash_id'],
                    'surplus_qty' => $goods->qty,
                    'stash_surplus_qty' => $stash_surplus_qty,
                    'cost_price' => $goods->cost_price,
                    'cost_amount' => bcmul($goods->cost_price, $goods_qty[$key], 2),
                    'price' => $goods_price[$key] ?? 0,
                    'amount' => bcmul($goods_price[$key], $goods_qty[$key], 2) ?? 0,
                    'remarks' => $remarks[$key] ?? '',
                    'status' => Finance::STATUS_QTC,
                ]);
            }
        }

        $info = $this->where('order_no', $params['order_no'])->find();
        if (!empty($info)) {
            $info->save([
                'order_no' => $params['order_no'],
                'date' => $params['date'],
                'admin_id' => $params['admin_id'] ?? 0,
                'unit_id' => $params['unit_id'],
                'staff_id' => $params['staff_id'] ?? 0,
                'stash_id' => $params['stash_id'],
                'remarks' => $params['remarks'],
                'price' => $price,
                'status' => Finance::STATUS_QTC,
            ]);
        }

        $financeModel = new Finance();
        $finance = $financeModel->where('order_no', $params['order_no'])->find();
        if (!empty($finance)) {
            $finance->save([
                'staff_id' => $params['staff_id'] ?? 0,
                'unit_id' => $params['unit_id'],
                'be' => 2,
                'price' => $price,
            ]);
        }

        return true;
    }
}